package net.lijingye.education.service.impl;

import net.lijingye.education.dao.CourseDao;
import net.lijingye.education.entity.Course;
import net.lijingye.education.entity.Source;
import net.lijingye.education.entity.Topic;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Column;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LijingYe on 2016/4/3.
 */
@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    public Course getCourseById(String courseId) throws DaoException, ObjectNotExistsException {
        Course course = courseDao.getById(courseId);
        if (null == course) {
            throw new ObjectNotExistsException("Course `" + courseId + "`");
        }
        return course;
    }

    public List<Source> getCourseBookListById(String courseId) throws DaoException, ObjectNotExistsException {
        return getCourseSourceListByTypeAndId(courseId, 1);
    }

    public List<Source> getCourseExamListById(String courseId) throws DaoException, ObjectNotExistsException {
        return getCourseSourceListByTypeAndId(courseId, 2);
    }

    public List<Source> getCourseInterviewListById(String courseId) throws DaoException, ObjectNotExistsException {
        return getCourseSourceListByTypeAndId(courseId, 3);
    }

    @Override
    public List<Topic> getCourseDifficultyListById(String courseId) throws DaoException, ObjectNotExistsException {
        return getCourseTopicListByTypeAndId(courseId, 1);
    }

    @Override
    public List<Topic> getCourseExpansionListById(String courseId) throws DaoException, ObjectNotExistsException {
        return getCourseTopicListByTypeAndId(courseId, 2);
    }

    private List<Source> getCourseSourceListByTypeAndId(String courseId, int type) throws DaoException, ObjectNotExistsException {
        Course course = courseDao.getById(courseId);
        List<Source> sourceList = course.getSourceList();
        List<Source> sourceWithTypeList = new ArrayList<>();
        sourceList.forEach(source -> {
            if (source.getType() == type) {
                sourceWithTypeList.add(source);
            }
        });
        return sourceWithTypeList;
    }

    private List<Topic> getCourseTopicListByTypeAndId(String courseId, int type) throws DaoException, ObjectNotExistsException {
        Course course = courseDao.getById(courseId);
        List<Topic> topicList = course.getTopicList();
        List<Topic> topicWithTypeList = new ArrayList<>();
        topicList.forEach(topic -> {
            if (topic.getType() == type) {
                topicWithTypeList.add(topic);
            }
        });
        return topicWithTypeList;
    }
}
